#-*-coding:utf-8-*-
'''
Created on 2013-3-28
C:/Users/zhaoyh/workspace/sysmanager/mainwindow.py
@author: zhaoyh
'''
from PyQt4 import QtCore, QtGui
from ui_mainwindows import Ui_MainWindow
from ui_skinpopup import Ui_skinpopup
from ui_jcw  import Ui_jcw
from ui_bd   import Ui_bdw

import inifile 
try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    _fromUtf8 = lambda s: s
class main(QtGui.QMainWindow,Ui_MainWindow):
    m_bMaxWin=False    
    def __init__(self, parent=None):
        super(main, self).__init__(parent)
        self.setupUi(self)
        self.setWindowFlags(QtCore.Qt.FramelessWindowHint)
        self.skinpopup=QtGui.QWidget()
        self.skinpopupui = Ui_skinpopup()
        self.skinpopupui.setupUi(self.skinpopup)
        self.skinpopup.setWindowFlags(QtCore.Qt.Popup)    
        self.frame.setStyleSheet(_fromUtf8(inifile.getvalue("skin", "skin", "border-image: url(:/skin/img/skin/skin01.jpg);")))
        self.bdw = QtGui.QWidget()
        self.bdwui=Ui_bdw()
        self.bdwui.setupUi(self.bdw)
        self.bdw.setObjectName(_fromUtf8("bdw"))
        self.mainstackedWidget.addWidget(self.bdw)
        
        self.checkw = QtGui.QWidget()
        self.checkingui=Ui_jcw()
        self.checkingui.setupUi(self.checkw)
        self.checkw.setObjectName(_fromUtf8("checkw"))
        self.mainstackedWidget.addWidget(self.checkw)
        self.mainstackedWidget.setCurrentWidget(self.checkw)
        
        
        self.mainstackedWidget.setCurrentWidget(self.checkw)
        QtCore.QObject.connect(self.bdtbtn,QtCore.SIGNAL(_fromUtf8("clicked()")), self.shangemainstackedWidget)
        QtCore.QObject.connect(self.jctbtn,QtCore.SIGNAL(_fromUtf8("clicked()")), self.shangemainstackedWidget)
        
      
        self.createContextMenu()
        QtCore.QObject.connect(self.closebtn, QtCore.SIGNAL(_fromUtf8("clicked()")), self.close)
        QtCore.QObject.connect(self.fulltbn, QtCore.SIGNAL(_fromUtf8("clicked()")), self.maxmin)
        QtCore.QObject.connect(self.titleskin, QtCore.SIGNAL(_fromUtf8("clicked()")), self.skinmenu)
        QtCore.QObject.connect(self.skinpopupui.skinindex01, QtCore.SIGNAL(_fromUtf8("clicked()")), self.changeskin)
        QtCore.QObject.connect(self.skinpopupui.skinindex02, QtCore.SIGNAL(_fromUtf8("clicked()")), self.changeskin)
        QtCore.QObject.connect(self.skinpopupui.skinindex03, QtCore.SIGNAL(_fromUtf8("clicked()")), self.changeskin)
        QtCore.QObject.connect(self.skinpopupui.skinindex04, QtCore.SIGNAL(_fromUtf8("clicked()")), self.changeskin)
        QtCore.QObject.connect(self.skinpopupui.skinindex05, QtCore.SIGNAL(_fromUtf8("clicked()")), self.changeskin)
        QtCore.QObject.connect(self.skinpopupui.skinindex06, QtCore.SIGNAL(_fromUtf8("clicked()")), self.changeskin)
        QtCore.QObject.connect(self.titlemenu, QtCore.SIGNAL(_fromUtf8("clicked()")), self.showContextMenu)


    def mouseDoubleClickEvent(self, e):
        '重载实现双击最大化'
        if (e.y()<= self.TitleBar.height() and e.button() == QtCore.Qt.LeftButton):
            self.maxmin()
    def mousePressEvent(self,e):
        '重载实现拖动'
        self.m_bLeftButtonPress=False
        if (e.y()<= self.TitleBar.height() and e.button() == QtCore.Qt.LeftButton):
            self.m_bLeftButtonPress=True
            self.dragPosition = e.globalPos() - self.frameGeometry().topLeft()
        else:
            e.ignore()
    def mouseMoveEvent(self,e):
        '重载实现拖动'
        try:
            if e.buttons() & QtCore.Qt.LeftButton & self.m_bLeftButtonPress:
                self.move(e.globalPos()-self.dragPosition)
                e.accept()
        except AttributeError:
            pass
    def changeskin(self):
        sender=self.sender()
        self.frame.setStyleSheet(sender.styleSheet())
        inifile.setvalue("skin", "skin", sender.styleSheet())

    def skinmenu(self):
        x = QtGui.QCursor.pos().x()-self.skinpopup.width()-self.titleskin.width()
        y=QtGui.QCursor.pos().y()
        self.skinpopup.move(x,y)
        self.skinpopup.show()
    def maxmin(self):
        if not self.m_bMaxWin:
            self.m_rectRestoreWindow = self.geometry()
            self.setGeometry(QtGui.QApplication.desktop().availableGeometry())
            self.fulltbn.setStyleSheet(_fromUtf8("QPushButton:hover{border-image: url(:/title/img/title/sys_button_restore1.png);）}\
            QPushButton:!hover{border-image: url(:/title/img/title/sys_button_restore0.png);）}"))           
        else:
            self.setGeometry(self.m_rectRestoreWindow)
            self.fulltbn.setStyleSheet(_fromUtf8("QPushButton:hover{border-image: url(:/title/img/title/sys_button_max1.png);）}\
            QPushButton:!hover{border-image: url(:/title/img/title/sys_button_max0.png);）}"))

        self.m_bMaxWin = not self.m_bMaxWin
        self.fulltbn.max=self.m_bMaxWin 
            #self.showFullScreen()
    def createContextMenu(self):
        '''
            创建右键菜单
        '''
        # 必须将ContextMenuPolicy设置为Qt.CustomContextMenu
        # 否则无法使用customContextMenuRequested信号
        self.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)  
        # 创建QMenu
        self.contextMenu = QtGui.QMenu(self)
        self.actionabout = self.contextMenu.addAction(u'关于')
        self.actionupdate = self.contextMenu.addAction(u'检查更新')
        self.actionsetting = self.contextMenu.addAction(u'设置')
        self.actionabout.triggered.connect(self.actionHandler)
        self.actionupdate.triggered.connect(self.actionHandler)
        self.actionsetting.triggered.connect(self.actionHandler)


    def showContextMenu(self):
        '''
        右键点击时调用的函数
        '''
        # 菜单显示前，将它移动到鼠标点击的位置
        x = QtGui.QCursor.pos().x()-120
        y=QtGui.QCursor.pos().y()+6
        self.contextMenu.move(x,y)
        self.contextMenu.show()


    def actionHandler(self):
        '''
        菜单中的具体action调用的函数
        '''
        QtGui.QDesktopServices.openUrl(QtCore.QUrl("http://www.yonyou.com"))
    def shangemainstackedWidget(self):
        sender=self.sender()
        if sender==self.jctbtn:
            w=self.checkw
        elif sender==self.bdtbtn:
            w=self.bdw

        self.mainstackedWidget.setCurrentWidget(w)
    


import qrc_rc
if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    windows =main()
    windows.show()
    sys.exit(app.exec_())